#include "CTestingWnd.h"

#include <QMessageBox>

#include "CDialog.h"
#include "zhilaiSDK/ZLDoorControlBoard.h"
#include "zhilaiSDK/ZLMonitorControlBoard.h"
#include "zhilaiSDK/zhilaiSDK.h"
#include "Version.h"

CTestingWnd::CTestingWnd(QWidget* parent) :
    QWidget(parent)
{
    ui.setupUi(this);
    setWindowTitle(tr("zhilaiSDK Demo") + " " + PROJECT_VERSION);
}

CTestingWnd::~CTestingWnd()
{
}

void CTestingWnd::on_btn_initialize_clicked()
{
    auto ret = ZLInitialize();
    if (ret != 0)
    {
        QMessageBox::information(this, tr("Error"), tr("Initialize zhilaiSDK failed"));
        return;
    }

    auto count = ZLDcbGetBoxCount();
    ui.combo_boxno->clear();
    for (int i = 0; i < count; ++i)
    {
        ui.combo_boxno->addItem(QString::number(i + 1), i + 1);
    }
}

void CTestingWnd::on_btn_uninitialize_clicked()
{
    ZLUninitialize();
}

void CTestingWnd::on_btn_open_clicked()
{
    auto number = ui.combo_boxno->currentData().toInt();
    auto ret = ZLDcbOpenLocker(number);
    if (ret != ZLResult_Success)
    {
        QMessageBox::information(this, tr("Error"), tr("Open box failed: %2").arg(ret));
        return;
    }

    QMessageBox::information(this, tr("Succsss"), tr("Open box success"));
}

void CTestingWnd::on_btn_openall_clicked()
{
    QString content;
    content.append(tr("Open all box result:") + "\n");
    auto count = ZLDcbGetBoxCount();
    if (count >= 1)
    {
        for (int i = 1; i <= count; ++i)
        {
            auto ret = ZLDcbOpenLocker(i);
            auto result = ret == ZLResult_Success ? tr("Success") : QString::number(ret);
            content.append(QString::asprintf("%3d: %-8s", i, result.toStdString().c_str()));
            content.append("\t");
            if ((i) % 5 == 0)
             {
                content.append("\n");
            }
        }   
    }
    else
    {
        content.append(tr("Unconnected! "));
    }


    CDialog::info(this, tr("Result"), content);
}

void CTestingWnd::on_btn_getdoorstatus_clicked()
{
    unsigned char status = 0x00;
    auto number = ui.combo_boxno->currentData().toInt();
    auto ret = ZLDcbGetDoorStatus(number, &status);
    if (ret != ZLResult_Success)
    {
        QMessageBox::information(this, tr("Error"), tr("Get door status failed: %1").arg(ret));
        return;
    }

    auto status_str = status == 0x00 ? tr("Closed") : tr("Opened");
    QMessageBox::information(this, tr("Success"), tr("Door status: %1").arg(status_str));
}

void CTestingWnd::on_btn_getalldoorstatus_clicked()
{
    unsigned char status[256] {0x00};
    size_t status_len = 256;
    auto ret = ZLDcbGetAllDoorStatus(status, &status_len);
    if (ret != ZLResult_Success)
    {
        QMessageBox::information(this, tr("Error"), tr("Get door status failed: %1").arg(ret));
        return;
    }

    auto pfunc = [](unsigned char status) {
        switch (status)
        {
            case 0x00:
                return tr("Closed");
            case 0x01:
                return tr("Opened");
            default:
                return tr("Error");
        }
    };
    QString content;
    for (size_t i = 0; i < status_len; ++i)
    {
        std::string status_str;
        switch (status[i])
        {
            case 0x00:
                tr("Closed");
        }
        content.append(QString::asprintf("%3d: %-7s", i + 1, pfunc(status[i]).toStdString().c_str()));
        content.append("\t");
        if ((i + 1) % 5 == 0)
        {
            content.append("\n");
        }
    }
    CDialog::info(this, tr("Result"), content);
}

void CTestingWnd::on_btn_getitemstatus_clicked()
{
    unsigned char status = 0x00;
    auto number = ui.combo_boxno->currentData().toInt();
    auto ret = ZLDcbGetItemStatus(number, &status);
    if (ret != ZLResult_Success)
    {
        QMessageBox::information(this, tr("Error"), tr("Get item status failed: %1").arg(ret));
        return;
    }

    auto status_str = status == 0x00 ? tr("Empty") : tr("Full");
    QMessageBox::information(this, tr("Success"), tr("Door status: %1").arg(status_str));
}

void CTestingWnd::on_btn_getallitemstatus_clicked()
{
    unsigned char status[256] {0x00};
    size_t status_len = 256;
    auto ret = ZLDcbGetAllItemStatus(status, &status_len);
    if (ret != ZLResult_Success)
    {
        QMessageBox::information(this, tr("Error"), tr("Get door status failed: %1").arg(ret));
        return;
    }
    auto pfunc = [](unsigned char s) {
        switch (s)
        {
            case 0x00:
                return tr("Empty");
            case 0x01:
                return tr("Full");
            default:
                return tr("Error");
        }
    };
    QString content;
    for (size_t i = 0; i < status_len; ++i)
    {
        content.append(QString::asprintf("%3d: %-7s", i + 1, pfunc(status[i]).toStdString().c_str()));
        content.append("\t");
        if ((i + 1) % 5 == 0)
        {
            content.append("\n");
        }
    }
    CDialog::info(this, tr("Result"), content);
}

void CTestingWnd::on_btn_getversion1_clicked()
{
    char version[2560] {0x00};
    auto ret = ZLDcbGetFirmwareVersion(version);
    if (ret != ZLResult_Success)
    {
        QMessageBox::information(this, tr("Error"), tr("Get firmware version failed: %1").arg(ret));
        return;
    }

    QMessageBox::information(this, tr("Result"), QString(version));
}

void CTestingWnd::on_btn_setthreshold_clicked()
{
    int32_t upper_limit = ui.edt_upper1->text().toDouble() * 100.00;
    int32_t lower_limit = ui.edt_lower1->text().toDouble() * 100.00;
    auto ret = ZLMcbSetTemperatureThreshold(upper_limit, lower_limit);
    if (ret != ZLResult_Success)
    {
        QMessageBox::information(this, tr("Error"), tr("Set temperature threshold failed: %1").arg(ret));
        return;
    }

    QMessageBox::information(this, tr("Success"), tr("Set temperature threshold success"));
}

void CTestingWnd::on_btn_getthreshold_clicked()
{
    int32_t upper_limit(0), lower_limit(0);
    auto ret = ZLMcbGetTemperatureThreshold(&upper_limit, &lower_limit);
    if (ret != ZLResult_Success)
    {
        QMessageBox::information(this, tr("Error"), tr("Get temperature threshold failed: %1").arg(ret));
        return;
    }

    ui.edt_upper2->setText(QString::number((double)upper_limit / 100.00, 'f', 2));
    ui.edt_lower2->setText(QString::number((double)lower_limit / 100.00, 'f', 2));
    QMessageBox::information(this, tr("Success"), tr("Get temperature threshold success"));
}

void CTestingWnd::on_btn_enable_fan_clicked()
{
    auto ret = ZLMcbEnableFan(0x01);
    if (ret != ZLResult_Success)
    {
        QMessageBox::information(this, tr("Error"), tr("Enable fan failed: %1").arg(ret));
        return;
    }

    QMessageBox::information(this, tr("Success"), tr("Enable fan success"));
}

void CTestingWnd::on_btn_disable_fan_clicked()
{
    auto ret = ZLMcbEnableFan(0x00);
    if (ret != ZLResult_Success)
    {
        QMessageBox::information(this, tr("Error"), tr("Disable fan failed: %1").arg(ret));
        return;
    }

    QMessageBox::information(this, tr("Success"), tr("Disable fan success"));
}

void CTestingWnd::on_btn_enable_lamp_clicked()
{
    auto ret = ZLMcbEnableLamp(0x02);
    if (ret != ZLResult_Success)
    {
        QMessageBox::information(this, tr("Error"), tr("Enable lamp failed: %1").arg(ret));
        return;
    }

    QMessageBox::information(this, tr("Success"), tr("Enable lamp success"));
}

void CTestingWnd::on_btn_disable_lamp_clicked()
{
    auto ret = ZLMcbEnableLamp(0x03);
    if (ret != ZLResult_Success)
    {
        QMessageBox::information(this, tr("Error"), tr("Disable lamp failed: %1").arg(ret));
        return;
    }

    QMessageBox::information(this, tr("Success"), tr("Disable lamp success"));
}

void CTestingWnd::on_btn_enable_lamp_2_clicked()
{
    auto ret = ZLMcbEnableLamp(0x01);
    if (ret != ZLResult_Success)
    {
        QMessageBox::information(this, tr("Error"), tr("Enable lamp failed: %1").arg(ret));
        return;
    }

    QMessageBox::information(this, tr("Success"), tr("Enable lamp success"));
}

void CTestingWnd::on_btn_disable_lamp_2_clicked()
{
    auto ret = ZLMcbEnableLamp(0x00);
    if (ret != ZLResult_Success)
    {
        QMessageBox::information(this, tr("Error"), tr("Enable lamp failed: %1").arg(ret));
        return;
    }

    QMessageBox::information(this, tr("Success"), tr("Enable lamp success"));
}

void CTestingWnd::on_btn_reboot_dcb_clicked()
{
    auto ret = ZLMcbResetDcb();
    if (ret != ZLResult_Success)
    {
        QMessageBox::information(this, tr("Error"), tr("Reboot door control board failed: %1").arg(ret));
        return;
    }

    QMessageBox::information(this, tr("Success"), tr("Reboot door control board success"));
}

void CTestingWnd::on_btn_reboot_system_clicked()
{
    auto ret = ZLMcbResetSystem();
    if (ret != ZLResult_Success)
    {
        QMessageBox::information(this, tr("Error"), tr("Reboot system failed: %1").arg(ret));
        return;
    }

    QMessageBox::information(this, tr("Success"), tr("Reboot system success"));
}

void CTestingWnd::on_btn_enable_power_monitoring_clicked()
{
    auto ret = ZLMcbEnablePowerMonitoring(0x01);
    if (ret != ZLResult_Success)
    {
        QMessageBox::information(this, tr("Error"), tr("Enable power monitoring failed: %1").arg(ret));
        return;
    }

    QMessageBox::information(this, tr("Success"), tr("Enable power monitoring success"));
}

void CTestingWnd::on_btn_disable_power_monitoring_clicked()
{
    auto ret = ZLMcbEnablePowerMonitoring(0x00);
    if (ret != ZLResult_Success)
    {
        QMessageBox::information(this, tr("Error"), tr("Disable power monitoring failed: %1").arg(ret));
        return;
    }

    QMessageBox::information(this, tr("Success"), tr("Disable power monitoring success"));
}

void CTestingWnd::on_btn_getversion2_clicked()
{
    char version[256] {0x00};
    auto ret = ZLMcbGetFirmwareVersion(version);
    if (ret != ZLResult_Success)
    {
        QMessageBox::information(this, tr("Error"), tr("Get version failed: %1").arg(ret));
        return;
    }

    QMessageBox::information(this, tr("Success"), tr("Version: %1").arg(version));
}

void CTestingWnd::on_btn_getTemperature_clicked()
{
    int32_t temperature = 0;
    auto ret = ZLMcbGetTemperature(&temperature);
    if (ret != ZLResult_Success)
    {
        QMessageBox::information(this, tr("Error"), tr("Get Temperature failed: %1").arg(ret));
        return;
    }

    QMessageBox::information(this, tr("Success"), tr("Temperature: %1").arg(QString::number((double)temperature / 100.00, 'f', 2)));
}

void CTestingWnd::on_btn_reset_router_clicked()
{
    auto ret = ZLMcbResetRouter();
    if (ret != ZLResult_Success)
    {
        QMessageBox::information(this, tr("Error"), tr("Reset Router failed: %1").arg(ret));
        return;
    }

    QMessageBox::information(this, tr("Success"), tr("Reset Router success"));
}

void CTestingWnd::on_btn_heart_beat_clicked()
{
    auto ret = ZLMcbHeartBeat();
    if (ret != ZLResult_Success)
    {
        QMessageBox::information(this, tr("Error"), tr("Send Heart Beat failed: %1").arg(ret));
        return;
    }

    QMessageBox::information(this, tr("Success"), tr("Send Heart Beat success"));
}
